package com.yaokai.algorithm;

/**
 * 采用递归方法，找出数组中的最大值
 *
 */
public class Code01_GetMax {

    public static void main(String[] args) {
        int[] arr = {1,2,3,-1,-6,100};
        System.out.println("最大值为："+getMax(arr));
    }

    public static int getMax(int[] arr){
        if(arr==null||arr.length==0){
            return 0;
        }
        return process(arr ,0 ,arr.length-1);
    }

    private static int process(int[] arr, int l, int r) {
        if (l==r) {

            return arr[l];
        }
        int mid = l + ((r-l) >> 1);
        int leftMax = process(arr ,l ,mid);
        int rightMax = process(arr ,mid+1 ,r);

        return leftMax>rightMax?leftMax:rightMax;
    }

}
